package com.nightsoul.springboottest2.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class MailServiceImpl implements MailService {
	private static final Logger LOGGER = LoggerFactory.getLogger(MailServiceImpl.class);

	@Resource
	private JavaMailSender mailSender;
	@Value("${app.mail.from.address}")
	private String from;
	
	@Override
	public void sendSimpleMail(String to, String subject, String content) {
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		mailMessage.setFrom(this.from);
		mailMessage.setTo(to);
		mailMessage.setSubject(subject);
		mailMessage.setText(content);
		
		try {
			this.mailSender.send(mailMessage);
			LOGGER.info("Simple mail is sent");
		} catch (Exception e) {
			LOGGER.error("Simple mail sent failed", e);
		}
	}

}
